package cn.outsourcing.supermarket.controller;

import cn.outsourcing.supermarket.common.CommonPage;
import cn.outsourcing.supermarket.common.Result;
import cn.outsourcing.supermarket.common.constants.CommonConstants;
import cn.outsourcing.supermarket.common.utils.ResultUtils;
import cn.outsourcing.supermarket.controller.request.SubmitOrderRequest;
import cn.outsourcing.supermarket.dto.DeleteOrderDTO;
import cn.outsourcing.supermarket.dto.OrderConfirmDTO;
import cn.outsourcing.supermarket.service.HiolabsCartService;
import cn.outsourcing.supermarket.service.HiolabsOrderGoodsService;
import cn.outsourcing.supermarket.service.HiolabsOrderService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * 前端控制器
 *
 * @author gzkemays
 * @since 2021-10-20
 */
@RestController
@RequestMapping("/order")
public class HiolabsOrderController {
  @Resource private HiolabsOrderService orderService;
  @Resource private HiolabsCartService cartService;
  @Resource private HiolabsOrderGoodsService orderGoodsService;

  @GetMapping("/list")
  public Result getOrderList(CommonPage page) {
    return ResultUtils.simpleResult(orderService.getOrderListVo(page));
  }

  @GetMapping("/detail")
  public Result getOrderDetail(Integer orderId) {
    return ResultUtils.simpleResult(orderService.getOrderDetail(orderId));
  }

  @PostMapping("/delete")
  public Result deleteOrder(@RequestBody DeleteOrderDTO dto) {
    return ResultUtils.simpleResult(orderService.deleteOrder(dto));
  }

  @PostMapping("/submitOrder")
  public Result submitOrder(@RequestBody SubmitOrderRequest request) {
    if (CollectionUtils.isEmpty(request.getCartIds())) {
      return Result.builder().errno(CommonConstants.REQUEST_SUCCESS_CODE).errmsg("缺乏购物车id").build();
    }
    if (Objects.isNull(request.getChannel())) {
      return Result.builder()
          .errno(CommonConstants.REQUEST_SUCCESS_CODE)
          .errmsg("支付渠道参数错误")
          .build();
    }
    return ResultUtils.simpleResult(
        cartService.submitOrder(
            request.getCartIds(), request.getIsIntegral(), request.getChannel()));
  }

  @PostMapping("/submitOrder/balance")
  public Result submitOrderByBalance(@RequestBody SubmitOrderRequest request) {
    return ResultUtils.simpleResult(
        cartService.submitOrderByBalance(new BigDecimal(request.getBalance())));
  }

  @GetMapping("/orderGoods")
  public Result getOrderGoods(Integer orderId) {
    return ResultUtils.simpleResult(orderGoodsService.getOrderGoods(orderId));
  }

  @GetMapping("/orderCount")
  public Result getOrderCount() {
    return ResultUtils.simpleResult(orderService.getOrderCount());
  }

  @PostMapping("/cancel")
  public Result cancelOrder(@RequestBody DeleteOrderDTO dto) {
    return ResultUtils.simpleResult(orderService.cancelOrder(dto));
  }

  @PostMapping("/confirm")
  public Result confirm(@RequestBody OrderConfirmDTO dto) {
    return ResultUtils.simpleResult(orderService.confirm(dto.getOrderId()));
  }
}
